// pages/articlesList/articlesList.js
const tools = require('../../utils/util.js')
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isChoose: 0,
		category:[], //分类
		article:[],
		category_id: null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.getCategoryList()
		this.getArticleList()
		this.setData({
			articleList: app.globalData.articleList
		})
		console.log('--------s',this.data.articleList)

		let list = this.data.articleList;
		let tags=["全部"];
		if (list){
			let tagsArr=[];
			for(let i = 0;i < list.length; i++){
				list[list.length-1].tags=['vue.js'];
				tagsArr = tagsArr.concat(list[i].tags);
			}
			this.setData({
				tags: tags.concat(Array.from(new Set(tagsArr)))
			})
		}
		
		console.log(this.data.tags)
		
	},
	

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	onShow:function(){
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (res) {
			return{title:'文章分类'}
	},
	onShareTimeline:function(res){
		return{
			title:'转发到朋友圈',
			
		}
	},
	// 自定
	/**
	 * 文章详情
	 */
	showArticle: function (e) {
		// console.log(e)
		console.log('------0',e.currentTarget.dataset.id)		//文章id
		wx.request({
			url: 'https://xiaxueruo.com/api/article/list?article_id='+e.currentTarget.dataset.id,
			success: function(res){
				articleList: res.data.data.articleList
			}
		})
		// wx.navigateTo({
		// 	url: '../articleContent/articleContent?id=' + e.currentTarget.dataset.id,
		// })
	},
	/**
	 * 选择标签
	 */
	chooseTag: function(e){
		console.log('------category',e)
		let tag = e.currentTarget.dataset.tag;	//导航栏
		console.log('------1',tag)
		console.log('--category_id', tag.category_id)
		let category_id = tag.category_id
		let tagIndex = e.currentTarget.dataset.index;
		console.log('------2',tagIndex)		//导航下标

		this.setData({
			isChoose: tagIndex
		})
		if(tag=='全部'){
			this.setData({
				articleList: app.globalData.articleList
			})
			console.log('------3',articleList)		
		}else{
			this.getArticleList(category_id)
		}
	},


	/**
	 * 分类导航栏 
	 */
	 getCategoryList(){
    let that = this
    wx.request({
      url: 'https://xiaxueruo.com/api/category/list',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',//get或者post
      success: function (res) {
        console.log(res)//接口返回值
        console.log('1111111',res.data.data.categoryList)
        that.setData({
          category: res.data.data.categoryList
        })
      }
    })
	},
	
	 /**
		* 
		* @param {筛选分类获取文章列表内容} category_id 
		*/
	 getArticleList(category_id){
		let that = this
		
		let data = {}
		if (category_id > 0) {
			data.category_id = category_id
		}
    wx.request({
			url: 'https://xiaxueruo.com/api/article/list/',
			data: data,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',//get或者post
      success: function (res) {
				//时间戳的更改
				app.globalData.articleList = res.data.data.articleList
				for(let item of app.globalData.articleList){
					console.log('-------------tools',tools.formatTime(item.add_time))
					item.add_time = tools.formatTime(item.add_time)
					item.update_time = tools.formatTime(item.update_time)
				}
        console.log(res)//接口返回值
        console.log('1111111',res.data.data.articleList)
        console.log('22222',res.data.data.articleList[0].article_title)
        that.setData({
          article: res.data.data.articleList
        })
      }
    })
  },
})